﻿#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <list>
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_AvCoder/NetEngine_AVHelp/AVHelp_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_AvCoder/NetEngine_AVHelp/AVHelp_Error.h"

//g++ -std=gnu++11 -Wall -g TestFor_AVHelp.cpp -o TestFor_AVHelp -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_AvCoder -lNetEngine_BaseLib -lNetEngine_Core -lNetEngine_AVHelp -Wl,-rpath=./:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_AvCoder,--disable-new-dtags

void Test_MetaInfo()
{
    AVHELP_METADATA st_AVMetaData;
    list<AVHELP_METAINFO> stl_ListMetaInfo;
    LPCTSTR lpszSrcFile = "D:\\123.mp3";

    memset(&st_AVMetaData, '\0', sizeof(AVHELP_METADATA));
    if (!AVHelp_MetaInfo_Get(lpszSrcFile, &st_AVMetaData, &stl_ListMetaInfo))
    {
        return;
    }
}
void Test_PPS264Info()
{
    LPCTSTR lpszSrcFile = "1.264";
    int nSPSLen = 0;
    int nPPSLen = 0;
    UCHAR uszSPSBuffer[64];
    UCHAR uszPPSBuffer[64];
    UCHAR uszIDLeave[64];
    TCHAR uszFileBuffer[2048];

    memset(uszSPSBuffer, '\0', sizeof(uszSPSBuffer));
    memset(uszPPSBuffer, '\0', sizeof(uszPPSBuffer));
    memset(uszIDLeave, '\0', sizeof(uszIDLeave));
    memset(uszFileBuffer, '\0', sizeof(uszFileBuffer));
    FILE *pSt_File = fopen(lpszSrcFile, "rb");
    int nRet = fread(uszFileBuffer, 1, sizeof(uszFileBuffer), pSt_File);

    if (!AVHelp_MetaInfo_Get264Hdr(uszFileBuffer, nRet, uszSPSBuffer, uszPPSBuffer, uszIDLeave, &nSPSLen, &nPPSLen))
    {
        return;
    }
    printf("SPS-%d:", nSPSLen);
    for ( int i = 0; i < nSPSLen; i++)
    {
        printf("%02X ", uszSPSBuffer[i]);
    }
    printf("\nPPS-%d:", nPPSLen);
    for ( int i = 0; i < nPPSLen; i++)
    {
        printf("%02X ", uszPPSBuffer[i]);
    }

    TCHAR tszSPSDest[1024];
    TCHAR tszPPSDest[1024];

    memset(tszSPSDest, '\0', sizeof(tszSPSDest));
    memset(tszPPSDest, '\0', sizeof(tszPPSDest));
    //OPenSsl_Codec_Base64((LPCSTR)uszSPSBuffer, tszSPSDest, &nSPSLen);
    //OPenSsl_Codec_Base64((LPCSTR)uszPPSBuffer, tszPPSDest, &nPPSLen);
    printf("\n%s %s", tszSPSDest, tszPPSDest);
}
void Test_PPS265Info()
{
    LPCTSTR lpszSrcFile = "2.hevc";
    int nSPSLen = 0;
    int nPPSLen = 0;
    int nVPSLen = 0;
    UCHAR uszSPSBuffer[64];
    UCHAR uszPPSBuffer[64];
    UCHAR uszVPSBuffer[64];
    TCHAR uszFileBuffer[2048];

    memset(uszSPSBuffer, '\0', sizeof(uszSPSBuffer));
    memset(uszPPSBuffer, '\0', sizeof(uszPPSBuffer));
    memset(uszVPSBuffer, '\0', sizeof(uszVPSBuffer));
    memset(uszFileBuffer, '\0', sizeof(uszFileBuffer));
    FILE* pSt_File = fopen(lpszSrcFile, "rb");
    int nRet = fread(uszFileBuffer, 1, sizeof(uszFileBuffer), pSt_File);

    if (!AVHelp_MetaInfo_Get265Hdr(uszFileBuffer, nRet, uszVPSBuffer, uszSPSBuffer, uszPPSBuffer, &nVPSLen, &nSPSLen, &nPPSLen))
    {
        return;
    }

    printf("VPS-%d:", nVPSLen);
    for (int i = 0; i < nVPSLen; i++)
    {
        printf("%02X ", uszVPSBuffer[i]);
    }
    printf("\nSPS-%d:", nSPSLen);
    for (int i = 0; i < nSPSLen; i++)
    {
        printf("%02X ", uszSPSBuffer[i]);
    }
    printf("\nPPS-%d:", nPPSLen);
    for (int i = 0; i < nPPSLen; i++)
    {
        printf("%02X ", uszPPSBuffer[i]);
    }

    int nProspace = 0;
    int nProID = 0;
    int nFlags = 0;
    int nLevelID = 0;

    TCHAR tszICStr[1024];
    memset(tszICStr, '\0', sizeof(tszICStr));
    AVHelp_MetaInfo_Get265Paraset(uszVPSBuffer, nVPSLen, &nProspace, &nProID, &nFlags, &nLevelID, tszICStr);
    printf("\n%d %d %d %d %s\n", nProspace, nProID, nFlags, nLevelID, tszICStr);
}
void Test_AVList()
{
    list<AVHELP_DEVICEINFO> stl_ListAudioDevic;
    list<AVHELP_DEVICEINFO> stl_ListVideoDevice;
    AVHelp_Device_EnumDevice(&stl_ListAudioDevic, &stl_ListVideoDevice);

    list<AVHELP_DEVICEINFO>::const_iterator stl_ListAudioIterator = stl_ListAudioDevic.begin();
    for (;stl_ListAudioIterator != stl_ListAudioDevic.end();stl_ListAudioIterator++)
    {
        printf("%d %d %s\n", stl_ListAudioIterator->nCardNumber,stl_ListAudioIterator->nDeviceNumber,stl_ListAudioIterator->tszName);
    }
    printf("\n");

    list<AVHELP_DEVICEINFO>::const_iterator stl_ListVideoIterator = stl_ListVideoDevice.begin();
    for (; stl_ListVideoIterator != stl_ListVideoDevice.end(); stl_ListVideoIterator++)
    {
        printf("%d %d %s\n", stl_ListVideoIterator->nCardNumber,stl_ListVideoIterator->nDeviceNumber,stl_ListVideoIterator->tszName);
    }
    printf("\n");

    list<AVHELP_METAINFO> stl_ListDevice;
    AVHelp_Device_DeviceList(&stl_ListDevice);

    printf("AVHelp_Device_DeviceList:%lu\n", stl_ListDevice.size());
    list<AVHELP_METAINFO>::const_iterator stl_ListIterator = stl_ListDevice.begin();
    for (; stl_ListIterator != stl_ListDevice.end(); stl_ListIterator++)
    {
        printf("%s=%s\n", stl_ListIterator->tszKey, stl_ListIterator->tszValue);
    }
}
void Test_AudioInfo()
{
    LPCTSTR lpszSrcFile = "test.aac";
    int nChannel = 0;
    int nSample = 0;
    int nProfile = 0;
    int nConfig = 0;
    UCHAR uszFileBuffer[2048];
    memset(uszFileBuffer, '\0', sizeof(uszFileBuffer));
    FILE* pSt_File = fopen(lpszSrcFile, "rb");

    int nRet = fread(uszFileBuffer, 1, sizeof(uszFileBuffer), pSt_File);
    if (!AVHelp_MetaInfo_GetAACInfo(uszFileBuffer, nRet, &nChannel, &nSample, &nProfile, &nConfig))
    {
        return;
    }

    printf("Audio:%d %d %d %d\n", nChannel,nSample,nProfile,nConfig);
}
int main()
{
    Test_AVList();
    Test_PPS265Info();
    Test_PPS264Info();
    Test_AudioInfo();
    system("pause");
    return 0;
}
